package com.google.code.cakedroid.net;

import java.net.InetAddress;

public class IPUtils {
	/*
	 * get the local host ip
	 */
	public static String getLocalHostIP(){
		try{
			InetAddress addr=InetAddress.getLocalHost();
			return addr.getHostAddress();
		}catch(Exception e){
			return "";
		}
	}
	
	/*
	 * get the local host name
	 */
	public static String getLocalHostName(){
		try{
			InetAddress addr=InetAddress.getLocalHost();
			return addr.getHostName();
		}catch(Exception e){
			return "";
		}
	}
	
	/*
	 * get all host ips by the given hostName
	 */
	public static String[] getAllHostIPs(String hostName){
		String[] ips=null;
		try{
			InetAddress[] addrs=InetAddress.getAllByName(hostName);
			if(null!=addrs){
				ips=new String[addrs.length];
				for(int i=0;i<addrs.length;i++){
					ips[i]=addrs[i].getHostAddress();
				}
			}
		}catch(Exception e){
			ips=null;
		}
		return ips;
	}
}
